Универсальный робот
Автор
Fast robot
Now, let us improve our robot. Let us make it faster and teach it diagonal moves. For now we keep its sensors the same: this will lead to some wall collisions, but it's ok.
In [ ]:
fastrobot = Robot()
# Let's give it some size
fastrobot.size = 1//2
# Initialize sensors: can see straight forward and diagonally forward
push!(fastrobot.sensors, rel_point_sense(forward))
push!(fastrobot.sensors, rel_point_sense(forward+right))
push!(fastrobot.sensors, rel_point_sense(forward+left))
# Initialize move actions: can move in 6 forward directions, and fast!
push!(fastrobot.moves, (pos,dir) -> (pos+dir,dir))
push!(fastrobot.moves, (pos,dir) -> (pos+2*dir,dir))
push!(fastrobot.moves, (pos,dir) -> (pos+2*dir+rotate(dir,90),dir))
push!(fastrobot.moves, (pos,dir) -> (pos+2*dir+rotate(dir,-90),dir))
push!(fastrobot.moves, (pos,dir) -> (pos+dir+rotate(dir,90),dir))
push!(fastrobot.moves, (pos,dir) -> (pos+dir+rotate(dir,-90),dir))
# And rotate in place
push!(fastrobot.moves, (pos,dir) -> (pos,rotate(dir,90)))
push!(fastrobot.moves, (pos,dir) -> (pos,rotate(dir,-90)))
# Initialize memory
init_memory!(fastrobot, start_x, start_y, dir_x, dir_y, size(room)[1], size(room)[2])
# Set goal: find exit inside the room walls
replace!(fastrobot.map, wall => goal_check);
Note that it moves to the closest point in terms of the number of expected actions (rotation in place is also an action), but not it terms of distance.
In [ ]:
run_robot!(fastrobot, room)
Out[0]:
Let us give it directional sensors, which let it see far away in the directions it could want to move.
In [ ]:
#Directional sensor: in a straight line up to distx forward and disty sideways
function directional_sensor(distx::Union{Rational,Integer}, disty::Union{Rational,Integer})
return function(pos::Cind, dir::Cind, room::Matrix{Integer})
dx, dy = distx*[Tuple(dir)...] + disty*[Tuple(rotate(dir,90))...]
return check_collisions(pos, dx//1, dy//1, 0//1, room)[2]
end
end
# New sensors
fastrobot.sensors = []
push!(fastrobot.sensors, directional_sensor(20,0))
push!(fastrobot.sensors, directional_sensor(20,10))
push!(fastrobot.sensors, directional_sensor(20,-10))
push!(fastrobot.sensors, directional_sensor(20,20))
push!(fastrobot.sensors, directional_sensor(20,-20))
# Initialize memory
init_memory!(fastrobot, start_x, start_y, dir_x, dir_y, size(room)[1], size(room)[2])
# Set goal: find exit inside the room walls
replace!(fastrobot.map, wall => goal_check);
In [ ]:
run_robot!(fastrobot, room)
Out[0]:
Slow robot
Or we can deprive the robot of everything.
This robot can only move forward or rotate left. And has no sensors!
In [ ]:
slowrobot = Robot()
# Initialize move actions:
push!(slowrobot.moves, (pos,dir) -> (pos+dir,dir))
push!(slowrobot.moves, (pos,dir) -> (pos,rotate(dir,90)))
# Initialize memory
init_memory!(slowrobot, start_x, start_y, dir_x, dir_y, size(room)[1], size(room)[2])
# Set goal: find exit inside the room walls
replace!(slowrobot.map, wall => goal_check);
In [ ]:
run_robot!(slowrobot, room)
Out[0]:
Personally, I already feel sorry for it. Let's give it an omnisensor: it deserves.
In [ ]:
# Sensor which immediately sees the whole room
push!(slowrobot.sensors, (pos, dir, room) -> [CartesianIndices(room)...])
# Initialize memory
init_memory!(slowrobot, start_x, start_y, dir_x, dir_y, size(room)[1], size(room)[2])
# Set goal: find exit inside the room walls
replace!(slowrobot.map, wall => goal_check);
In [ ]:
run_robot!(slowrobot, room)
Out[0]:



